// ==UserScript==
// @name         鬼火查询怪额码子
// @namespace    http://tampermonkey.net/
// @version      2025.06.26
// @description  鬼火查询怪额码子
// @author       xx
// @match        https://pay.god.ghh6.org/LyQixDMrWn.php/channel/codelimit?*
// @grant        GM_xmlhttpRequest
// @grant        GM_addStyle
// @connect      pay.god.ghh6.org
// ==/UserScript==

(function () {
    'use strict';

    //定时任务
    function task() {
        const now = Date.now();
        const url = `http://pay.god.ghh6.org/LyQixDMrWn.php/channel/codelimit?addtabs=1&sort=id&order=desc&offset=0&limit=50&filter=%7B%7D&op=%7B%7D&_=${now.toString()}&sort=id&order=desc&offset=0&limit=50&filter=%7B%7D&op=%7B%7D`;
        GM_xmlhttpRequest({
            method: "GET",
            url: url,
            headers: {
                "Accept": "application/json, text/javascript, */*; q=0.01",
                "X-Requested-With": "XMLHttpRequest",
                //"Cookie": document.cookie, // 使用当前页面的 Cookie
                //"User-Agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/137.0.0.0 Safari/537.36"
            },
            onload: function (res) {
                try {
                    const data = JSON.parse(res.responseText);
                    // 遍历 rows
                    const rows = data.rows[0];
                    for (const key in rows) {
                        const list = rows[key];
                        if (!Array.isArray(list)) continue;

                        for (const item of list) {
                            const match = item.match(/金额:(\d+\.\d+)\s+失败次数:(\d+)/);
                            if (match) {
                                const amount = parseFloat(match[1]);
                                const count = parseInt(match[2]);
                                if (amount === 648 && count > 3) {
                                    if (Notification.permission !== "granted") {
                                        Notification.requestPermission();
                                    }

                                    new Notification("提示", {
                                        body: `金额为 648.00 的失败次数为 ${count}，请检查！`,
                                        icon: "https://example.com/icon.png", // 可选
                                    });
                                }
                            }
                        }
                    }
                } catch (e) {
                    console.log("JSON 解析错误:", e);

                }
            },
            onerror: function (e) {
                console.log("JSON 解析错误2:", e);

            }
        });
        console.log(`查询时间: ${new Date(now).toLocaleString()}`);
    }




    // 监听 `刷新` 按钮是否出现
    function waitForElements(selector, callback) {
        let interval = setInterval(() => {
            let elements = document.querySelectorAll(selector);
            if (elements.length > 0) {
                clearInterval(interval);
                callback(elements);
            }
        }, 500);
    }
    let times = null; // 提升到外部作用域，确保每次点击都操作的是同一个定时器
    // 开始or暂停
    function startOrPause() {
        const uploadBtn = document.querySelector(".upload-code-btn");
        if (uploadBtn) {
            if (uploadBtn.innerText === "开始") {
                uploadBtn.innerText = "暂停";
                //每3秒定时查询一次
                times = setInterval(task, 3000);
                // alert("开始查询");
                // 开始定时器任务
                // showUploadDialog();
            } else {
                uploadBtn.innerText = "开始";
                // 停止定时器任务
                clearInterval(times);
                // 暂停定时器任务
                // alert("已停止查询");


            }
        }
    }

    //  添加 `开始/暂停` 按钮
    function addUploadButton(buttons) {
        buttons.forEach(button => {
            if (!button.dataset.uploadAdded) {  // 防止重复添加
                button.dataset.uploadAdded = "true";

                let uploadBtn = document.createElement("a");
                uploadBtn.innerText = "开始";
                uploadBtn.href = "javascript:void(0);";
                uploadBtn.className = "btn btn-primary btn-xs upload-code-btn";
                uploadBtn.title = "开始";
                uploadBtn.onclick = () => startOrPause();

                button.parentNode.insertBefore(uploadBtn, button);
            }
        });
    }

    // 4. 添加样式
    GM_addStyle(`
        .upload-code-btn {
            margin-right: 5px;
            background: #007bff;
            color: white !important;
            border: none;
            padding: 5px 10px;
            cursor: pointer;
            border-radius: 4px;
            text-decoration: none;
        }
        .upload-code-btn:hover {
            background: #0056b3;
        }
        .upload-dialog {
            position: fixed;
            top: 50%;
            left: 50%;
            transform: translate(-50%, -50%);
            background: white;
            padding: 20px;
            box-shadow: 0 4px 8px rgba(0,0,0,0.2);
            border-radius: 8px;
            z-index: 1000;
            width: 300px;
        }
        .upload-dialog-content {
            display: flex;
            flex-direction: column;
        }
        .upload-dialog-content label {
            margin-top: 10px;
        }
        .upload-dialog-content input, .upload-dialog-content textarea {
            width: 100%;
            padding: 8px;
            margin-top: 5px;
            border: 1px solid #ccc;
            border-radius: 4px;
        }
        .dialog-buttons {
            display: flex;
            justify-content: space-between;
            margin-top: 15px;
        }
        .dialog-buttons button {
            padding: 5px 10px;
            border: none;
            cursor: pointer;
            border-radius: 4px;
        }
        #confirm-upload {
            background: #28a745;
            color: white;
        }
        #confirm-upload:hover {
            background: #218838;
        }
        #cancel-upload {
            background: #dc3545;
            color: white;
        }
        #cancel-upload:hover {
            background: #c82333;
        }
        .close-btn {
            position: absolute;
            top: 5px;
            right: 10px;
            cursor: pointer;
            font-size: 18px;
        }
    `);
    // 监测 `刷新` 按钮
    waitForElements("a.btn.btn-primary.btn-refresh", addUploadButton);

})();
